package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.EPass;
	import classes.model.VOUserInfo;
	
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	public class PopupFriend_popupcont extends Sprite
	{
		private var _fullpopup:MovieClip;
		private var _delPopup:MovieClip;
		private var _gameingPopup:MovieClip;
		private var _passPopup:MovieClip;
		private var _noRoomPopup:MovieClip;
		private var _activeClip:MovieClip;
		
		private var _popupSelected:int;
		private var _clickFriend:String;
		
		public function PopupFriend_popupcont()
		{
			setLayOut();
			defaultSetting();
			init();
		}
		private function setLayOut():void
		{
			this._fullpopup			= this.getChildByName('full_popup')		as MovieClip;
			this._delPopup			= this.getChildByName('delete_popup')	as MovieClip;
			this._gameingPopup		= this.getChildByName('gameing_popup')	as MovieClip;
			this._passPopup			= this.getChildByName('pass_popup')		as MovieClip;
			this._noRoomPopup		= this.getChildByName('no_room_popup')	as MovieClip;
		}
		private function defaultSetting():void
		{
			this.visible 								= false;
			
			this._fullpopup.buttonMode 					= true;
			this._gameingPopup.buttonMode 				= true;
			this._noRoomPopup.buttonMode				= true;
			
			_passPopup.incorrect_mc.visible 			= false;
			_passPopup.input_txt.displayAsPassword 		= true;
		}
		
		public function popupClose():void
		{
			//this.visible = false;
			//this.alpha   = 0;
			TweenMax.killTweensOf(this, false);
			TweenMax.to(this, 0.3, {autoAlpha:0, ease:Cubic.easeOut});
		}
		
		public function get popupSelected():int
		{
			return _popupSelected;
		}
		public function set clickFriend($id:String):void
		{
			this._clickFriend = $id;
		}
		
		private var _roomID:int;
		public function set roomID($param:int):void
		{
			this._roomID = $param;
		}
		
		public function set popupSelected($param:int):void
		{
			_popupSelected = $param;
			switch($param){
				case 0:
					//방이 꽉찼습니다.
					_activeClip = this._fullpopup;		
					this._delPopup.visible 		= false;
					this._gameingPopup.visible 	= false;
					this._passPopup.visible		= false;
					this._noRoomPopup.visible	= false;
					_activeClip.visible 		= true;
					break;
				case 1:
					//삭제하시겠습니까? 
					_activeClip = this._delPopup;
					this._fullpopup.visible 	= false;
					this._gameingPopup.visible 	= false;
					this._passPopup.visible		= false;
					this._noRoomPopup.visible	= false;
					_activeClip.visible 		= true;
					break;
				case 2:
					//게임 중입니다.
					_activeClip = this._gameingPopup;
					this._fullpopup.visible		= false;
					this._delPopup.visible 		= false;
					this._passPopup.visible		= false;
					this._noRoomPopup.visible	= false;
					_activeClip.visible 		= true;
					break;
				case 3:
					//패스워드 입력.
					_activeClip = this._passPopup;
					_passPopup.input_txt.text = "";
					_passPopup.incorrect_mc.visible = false;
					
					this._fullpopup.visible		= false;
					this._delPopup.visible 		= false;
					this._gameingPopup.visible 	= false;
					this._noRoomPopup.visible	= false;
					_activeClip.visible 		= true;
					break;
				case 4:
					//방이 존재하지 않습니다.
					_activeClip = this._gameingPopup;
					this._fullpopup.visible		= false;
					this._delPopup.visible 		= false;
					this._passPopup.visible		= false;
					this._noRoomPopup.visible	= true;
					_activeClip.visible 		= true;
					break;
				default :
					break;
			}
		}
		
		private function init():void
		{
			this._delPopup.ok_mc.addEventListener(MouseEvent.CLICK, 					delClick);
			this._delPopup.cancel_mc.addEventListener(MouseEvent.CLICK, 				delClick);
			this._delPopup.close_btn.addEventListener(MouseEvent.CLICK, 				delClick);
			
			this._passPopup.close_btn.addEventListener(MouseEvent.CLICK,				passClick);
			this._passPopup.cancel_mc.addEventListener(MouseEvent.CLICK,				passClick);
			this._passPopup.ok_mc.addEventListener(MouseEvent.CLICK,					passClick);
			
			this._passPopup.addEventListener(FocusEvent.FOCUS_IN,						onFocusInHandler);
			this._passPopup.addEventListener(FocusEvent.FOCUS_OUT,						onFocusOutHandler);
			
			this._noRoomPopup.addEventListener(MouseEvent.CLICK,						onClickClose);
			this._fullpopup.addEventListener(MouseEvent.CLICK,							onClickClose);
			this._gameingPopup.addEventListener(MouseEvent.CLICK,						onClickClose);
		}
		
		
		private function onFocusInHandler(e:FocusEvent):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,		onKeyDownHandler);
		}
		private function onFocusOutHandler(e:FocusEvent):void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,	onKeyDownHandler);	
		}
		private function onKeyDownHandler(e:KeyboardEvent):void
		{
			//trace(e.keyCode);
			if(e.keyCode == 13){
				passExecute();
			}
		}
		
		private function passClick(e:MouseEvent):void
		{
			if((e.currentTarget == _passPopup.close_btn) || (e.currentTarget == _passPopup.cancel_mc)){
				popupClose();
				_popupSelected = -1;
			}else if(e.currentTarget == _passPopup.ok_mc){
				passExecute();
			}
		}
	
	
		private function passExecute():void
		{
			dispatchEvent(new EPass(_passPopup.input_txt.text, _roomID, EPass.EVENT_PASSWORD));
		}
			
		public function incorrectPass():void
		{
			_passPopup.incorrect_mc.visible = true;
		}
		
		private function delClick(e:MouseEvent):void
		{
			if(e.currentTarget == _delPopup.ok_mc){
				/**
				 *
				 * 친구 삭제 샌드. 
				 * 
				 **/
				var obj:Object = {"data":[
										{
											"fr_id":_clickFriend,
											"mb_id":VOUserInfo.getDataInstance().data.id
										}
									]
									};
				
				//trace('>>'+obj.data[0].fr_id);
				DAOSendFlash.getLoadInstance().setData("friendDel",obj);
				
				popupClose();
				_popupSelected = -1;
			}else if((e.currentTarget == _delPopup.cancel_mc) ||(e.currentTarget == _delPopup.close_btn)){
				popupClose();
				_popupSelected = -1;
			}
		}
		private function onClickClose(e:MouseEvent):void
		{
			popupClose();
			_popupSelected = -1;
		}
		
		
	}
}